/**
 * 浮点数计算
 */
var cashConversionTools = {

    /**
     * 精确度规则
     */
    ACCURACY_1: "0",//精确到分(默认对厘进行四舍五入)
    ACCURACY_2: "10",//精确到角，分舍去
    ACCURACY_3: "11",//精确到角，分四舍五入
    ACCURACY_4: "12",//精确到角，分进位

    /**
     * 获取默认数
     * @param value
     * @returns {*}
     */
    getNumHasDefault: function (value) {
        var valueType = typeof (value);
        if (isNaN(value) || null == value) {
            value = 0.00;
        }
        return value;
    },

    /**
     * 浮点运算加
     * @param a
     * @param b
     */
    plus: function (a, b) {
        var c, d, e;
        try {
            c = a.toString().split(".")[1].length;
        } catch (f) {
            c = 0;
        }
        try {
            d = b.toString().split(".")[1].length;
        } catch (f) {
            d = 0;
        }
        return e = Math.pow(10, Math.max(c, d)), (cashConversionTools.multiply(a, e) + cashConversionTools.multiply(b, e)) / e;
    },
    /**
     * 浮点运算减
     * @param a
     * @param b
     */
    minus: function (a, b) {
        var c, d, e;
        try {
            c = a.toString().split(".")[1].length;
        } catch (f) {
            c = 0;
        }
        try {
            d = b.toString().split(".")[1].length;
        } catch (f) {
            d = 0;
        }
        return e = Math.pow(10, Math.max(c, d)), (cashConversionTools.multiply(a, e) - cashConversionTools.multiply(b, e)) / e;
    },
    /**
     * 浮点运算乘
     * @param a
     * @param b
     */
    multiply: function (a, b) {
        var c = 0,
            d = a.toString(),
            e = b.toString();
        try {
            c += d.split(".")[1].length;
        } catch (f) {
        }
        try {
            c += e.split(".")[1].length;
        } catch (f) {
        }
        return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
    },
    /**
     * 浮点运算除
     * @param a
     * @param b
     */
    divided: function (a, b) {
        var c, d, e = 0,
            f = 0;
        try {
            e = a.toString().split(".")[1].length;
        } catch (g) {
        }
        try {
            f = b.toString().split(".")[1].length;
        } catch (g) {
        }
        return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), cashConversionTools.multiply(c / d, Math.pow(10, f - e));
    },
    /**
     * 精确到几位有效数字
     * @param value
     * @param placeholder
     * @returns {*}
     */
    toDecimal: function (value, placeholder) {
        var f = parseFloat(value);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(value * Math.pow(10, placeholder)) / Math.pow(10, placeholder);
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + placeholder) {
            s += '0';
        }
        return s;
    },
    /**
     * 至少显示多少位有效数字
     * @param value
     * @param placeholder
     */
    showDecimal: function (value, placeholder) {
        var s = value.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + placeholder) {
            s += '0';
        }
        return s;
    },
    amtUpCase: function (value) {
        //转成字符串
        value = value + "";
        if (value == "") {
            return value;
        } else if (!/^[-]{0,1}(\d+)\.?\d*$/.test(value)) {
            alert("交款金额输入有误");
            return value;
        }
        var isFushu = '';
        if (value.substring(0, 1) == '-') {
            isFushu = '负';
            value = value.substring(1, value.length);
        }
        var index_true = -1;//小数点位置
        var value_xs = "";//小数点后面数据
        if (value.indexOf(".") != -1) {
            index_true = value.indexOf(".");
            //分小数和整数
            var value_zs = value.substring(0, index_true);
            value_xs = value.substring(index_true + 1, value.length);
            value = value_zs;
        }
        if (value_xs.length > 2) {
            value_xs = value_xs.substring(0, 2);
        }
        if (value.length > 8) {
            var lower1 = value.substring(0, value.length - 8);
            var lower2 = value.substring(value.length - 8, value.length - 4);
            var lower3 = value.substring(value.length - 4, value.length);
            var xs = "";
            var upper1 = monUp(lower1) + "亿";
            var upper2 = "";
            if (lower2 == "0000") {
                upper2 = "";
            } else if (lower2.indexOf("0") == 0) {
                upper2 = "零" + monUp(lower2) + "万";
            } else {
                upper2 = monUp(lower2) + "万";
            }
            var upper3 = "";
            if (lower3 == "0000") {
                if (value_xs == "") {
                    upper3 = "元整";
                } else {
                    upper3 = "元";
                }

            } else if (lower3.indexOf("0") == 0) {
                if (value_xs == "") {
                    upper3 = "零" + cashConversionTools.monUp(lower3) + "元整";
                } else {
                    upper3 = "零" + cashConversionTools.monUp(lower3) + "元";
                }
            } else {
                if (value_xs == "") {
                    upper3 = monUp(lower3) + "元整";
                } else {
                    upper3 = monUp(lower3) + "元";
                }
            }

            if (value_xs.length == 2) {
                xs = cashConversionTools.monUp(value_xs.substring(0, 1)) + "角" + cashConversionTools.monUp(value_xs.substring(1, 2)) + "分";
            } else if (value_xs.length == 1) {
                xs = cashConversionTools.monUp(value_xs.substring(0, 1)) + "角";
            }

            return isFushu + upper1 + upper2 + upper3 + xs;
        } else if (value.length > 4) {
            var lower1 = value.substring(0, value.length - 4);
            var lower2 = value.substring(value.length - 4, value.length);
            var upper1 = cashConversionTools.monUp(lower1) + "万";
            var upper2 = "";
            var xs = "";
            if (lower2 == "0000") {
                if (value_xs == "") {
                    upper2 = "元整";
                } else {
                    upper2 = "元";
                }
            } else if (lower2.indexOf("0") == 0) {
                if (value_xs == "") {
                    upper2 = "零" + cashConversionTools.monUp(lower2) + "元整";
                } else {
                    upper2 = "零" + cashConversionTools.monUp(lower2) + "元";
                }
            } else {
                if (value_xs == "") {
                    upper2 = cashConversionTools.monUp(lower2) + "元整";
                } else {
                    upper2 = cashConversionTools.monUp(lower2) + "元";
                }
            }

            if (value_xs.length == 2) {
                xs = cashConversionTools.monUp(value_xs.substring(0, 1)) + "角" + cashConversionTools.monUp(value_xs.substring(1, 2)) + "分";
            } else if (value_xs.length == 1) {
                xs = cashConversionTools.monUp(value_xs.substring(0, 1)) + "角";
            }

            return isFushu + upper1 + upper2 + xs;
        } else {
            var xs = "";
            if (value_xs.length == 2) {
                xs = cashConversionTools.monUp(value_xs.substring(0, 1)) + "角" + cashConversionTools.monUp(value_xs.substring(1, 2)) + "分";
            } else if (value_xs.length == 1) {
                xs = cashConversionTools.monUp(value_xs.substring(0, 1)) + "角";
            }
            if (value_xs == "") {
                return isFushu + cashConversionTools.monUp(value) + "元整";
            } else {
                return isFushu + cashConversionTools.monUp(value) + "元" + xs;
            }
        }
    },
    numUp: function (value) {
        switch (value) {
            case '0':
                return "零";
                break;
            case '1':
                return "壹";
                break;
            case '2':
                return "贰";
                break;
            case '3':
                return "叁";
                break;
            case '4':
                return "肆";
                break;
            case '5':
                return "伍";
                break;
            case '6':
                return "陆";
                break;
            case '7':
                return "柒";
                break;
            case '8':
                return "捌";
                break;
            case '9':
                return "玖";
                break;
        }
    },
    monUp: function (str) {
        //转成字符串
        str = str + "";
        //去首零
        while (str.length > 0 && str.substring(0, 1) == "0") {
            str = str.substring(1);
        }
        var sb = "";
        for (var i = 0; i < str.length; i++) {
            sb = sb + cashConversionTools.numUp(str.substring(i, i + 1));
        }
        if (sb.length == 0) {
            return "零";
        } else if (sb.length == 1) {
            return sb;
        } else if (sb.length == 2) {
            if (sb.substring(1, 2) == "零") {//例:10
                return sb.substring(0, 1) + "拾";
            } else {//例:11
                return sb.substring(0, 1) + "拾" + sb.substring(1, 2);
            }
        } else if (sb.length == 3) {
            if (sb.indexOf("零零") != -1) {//例:100
                sb = sb.substring(0, 1);
                return sb + "佰";
            } else if (sb.substring(1, 2) == "零") {//例:101
                return sb.substring(0, 1) + "佰" + sb.substring(1);
            } else if (sb.substring(2, 3) == "零") {//例:110
                return sb.substring(0, 1) + "佰" + sb.substring(1, 2) + "拾";
            } else {//例:111
                return sb.substring(0, 1) + "佰" + sb.substring(1, 2) + "拾" + sb.substring(2, 3);
            }
        } else if (sb.length == 4) {
            if (sb.indexOf("零零零") != -1) {//例:1000
                return sb.substring(0, 1) + "仟";
            } else if (sb.indexOf("零零") == 1) {//例:1001
                return sb.substring(0, 1) + "仟零" + sb.substring(3, 4);
            } else if (sb.indexOf("零零") == 2) {//例:1100
                return sb.substring(0, 1) + "仟" + sb.substring(1, 2) + "佰";
            } else if (sb.substring(1, 2) == "零" && sb.substring(3, 4) == "零") {//例:1010
                return sb.substring(0, 1) + "仟零" + sb.substring(2, 3) + "拾";
            } else if (sb.substring(1, 2) == "零") {//例:1011
                return sb.substring(0, 1) + "仟零" + sb.substring(2, 3) + "拾" + sb.substring(3, 4);
            } else if (sb.substring(2, 3) == "零") {//例:1101
                return sb.substring(0, 1) + "仟" + sb.substring(1, 2) + "佰零" + sb.substring(3, 4);
            } else if (sb.substring(3, 4) == "零") {//例:1110
                return sb.substring(0, 1) + "仟" + sb.substring(1, 2) + "佰" + sb.substring(2, 3) + "拾";
            } else {//例:1111
                return sb.substring(0, 1) + "仟" + sb.substring(1, 2) + "佰" + sb.substring(2, 3) + "拾" + sb.substring(3, 4);
            }
        }
    },
    /**
     * 经过精确度处理后得到的数值
     * @param num 原始数值,默认正数小数点后面为分
     * @param accuracy 精确度类型
     * @param deviationSign 尾差标识，若为true则返回真实尾差
     */
    getActualNumber: function (num, accuracy, deviationSign) {
        var _number = cashConversionTools.getNumHasDefault(num);
        var _actualNumber = '0.00';
        var _deviation = '0.00';
        switch (accuracy) {
            case  cashConversionTools.ACCURACY_1:
                _actualNumber = Math.round(_number * 100) / 100;
                break;
            case  cashConversionTools.ACCURACY_2:
                _actualNumber = Math.floor(_number * 10) / 10;
                break;
            case  cashConversionTools.ACCURACY_3:
                _actualNumber = Math.round(_number * 10) / 10;
                break;
            case  cashConversionTools.ACCURACY_4:
                _actualNumber = Math.ceil(_number * 10) / 10;
                break;
            default:
                break;
        }
        if (deviationSign) {
            return cashConversionTools.toDecimal(num - _actualNumber, 4)
        }
        return _actualNumber;
    }


}